//
//  UIBarButtonItem+CXItem.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/27.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "UIBarButtonItem+CXItem.h"

@implementation UIBarButtonItem (CXItem)

+(UIBarButtonItem *)itemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action{
    //设置左边按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:highlightedImage forState:UIControlStateHighlighted];
    //设置尺寸
    [button sizeToFit];
    
    //添加点击事件
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIView *containView = [[UIView alloc] initWithFrame:button.bounds];
    [containView addSubview:button];
    
    //设置点击区域与设置不符：把UIButton包装成UIBarButtonItem的时候，会导致按钮点击区域变大
    return [[UIBarButtonItem alloc] initWithCustomView:containView];
}

+(UIBarButtonItem *)itemWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action{
    //设置左边按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateSelected];
    //设置尺寸
    [button sizeToFit];
    
    //添加点击事件
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIView *containView = [[UIView alloc] initWithFrame:button.bounds];
    [containView addSubview:button];
    
    //设置点击区域与设置不符：把UIButton包装成UIBarButtonItem的时候，会导致按钮点击区域变大
    return [[UIBarButtonItem alloc] initWithCustomView:containView];
}


+(UIBarButtonItem *)backItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action title:(NSString *)title{
    //设置导航条左侧的按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:title forState:UIControlStateNormal];
    [backBtn setImage:image forState:UIControlStateNormal];
    [backBtn setImage:highlightedImage forState:UIControlStateHighlighted];
    [backBtn setTitleColor:[UIColor colorWithHexString:@"#9B9A9B"] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor colorWithHexString:@"#E3A142"] forState:UIControlStateHighlighted];
    
    //千万不要忘记设置尺寸
    [backBtn sizeToFit];
    
    //
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    
    [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

@end
